<?php
$form = $this->beginWidget('CActiveForm', array(
    'id' => 'videoyoutube-form',
    'enableClientValidation' => true,
    'enableAjaxValidation'   => true,
    'clientOptions'          => array(
        'validateOnSubmit' => true,
    ),
    'htmlOptions'   => array(
        'enctype' => 'multipart/form-data',
        'class'   => 'form-horizontal',
    )
));
?>

<div class="box">
    <header class="dark">
        <div class="icons"><i class="icon-file-text-alt"></i></div>
        <h5><?php echo ucfirst($mode) ?> Video Youtube</h5>
    </header>

    <div class="alert">
        <button class="close" data-dismiss="alert">×</button>
        Fields with <span class="required">*</span> are required.
    </div>

    <?php if ($id) { ?>
        <?php $error = $form->error($model, 'title'); ?>
        <div class="control-group <?php echo strip_tags($error) ? 'error' : '' ?>">
            <?php echo $form->labelEx($model, 'title', array('class' => 'control-label')); ?>
            <div class="controls">
                <?php echo $form->textField($model, 'title', array('class' => 'input-xlarge')); ?>
                <p class="help-block"><?php echo $error; ?></p>
            </div>
        </div>

        <?php $error = $form->error($model, 'file'); ?>
        <div class="control-group <?php echo strip_tags($error) ? 'error' : '' ?>">
            <?php echo $form->labelEx($model, 'file', array('class' => 'control-label')); ?>
            <div class="controls">
                <?php echo $form->textField($model, 'file', array('class' => 'input-xlarge')); ?>
                <p class="help-block"><?php echo $error; ?></p>
            </div>
        </div>

        <?php $error = $form->error($model, 'image'); ?>
        <div class="control-group <?php echo strip_tags($error) ? 'error' : '' ?>">
            <?php echo $form->labelEx($model, 'image', array('class' => 'control-label')); ?>
            <div class="controls">
                <?php echo $form->hiddenField($model, "image"); ?>
                <?php $this->widget('CMultiFileUpload', array(
                        'name'      => 'image',
                        'accept'    => 'jpeg|jpg|gif|png', // useful for verifying files
                        'duplicate' => 'Duplicate file!', // useful, i think
                        'denied'    => 'Invalid file type',
                        // useful, i think
                    )); ?>
                <p class="help-block"><?php echo $error; ?></p>

                <?php if ($model->image):
                    $image = explode(',', $model->image);
                    foreach ($image as $k => $file):
                        ?>
                        <div class="clearfix"></div>
                        <div><?php echo $file; ?> &nbsp;
                            <a href="javascript:;" onclick="javascript:removeFile('<?php echo $file; ?>', <?php echo $k; ?>)" title="Remove" class="remove_<?php echo $k; ?>">Remove File</a></div>
                        <div class="clearfix"></div>
                    <?php
                    endforeach;
                endif;?>
            </div>
        </div>
    <?php } else { ?>
        <?php $error = $form->error($model, 'title'); ?>
        <div class="control-group <?php echo strip_tags($error) ? 'error' : '' ?>">
            <?php echo $form->labelEx($model, 'title', array('class' => 'control-label')); ?>
            <div class="controls">
                <?php echo $form->textField($model, 'title', array('class' => 'input-xlarge')); ?>
                <p class="help-block"><?php echo $error; ?></p>
            </div>
        </div>

        <?php $error = $form->error($model, 'linkFile'); ?>
        <div class="control-group <?php echo strip_tags($error) ? 'error' : '' ?>">
            <?php echo $form->labelEx($model, 'linkFile', array('class' => 'control-label')); ?>
            <div class="controls">
                <?php echo $form->textField($model, 'linkFile', array('class' => 'input-xlarge')); ?>
                <p class="help-block"><?php echo $error; ?></p>
            </div>
        </div>

        <?php $error = $form->error($model, 'image'); ?>
        <div class="control-group <?php echo strip_tags($error) ? 'error' : '' ?>">
            <?php echo $form->labelEx($model, 'image', array('class' => 'control-label')); ?>
            <div class="controls">
                <?php echo $form->hiddenField($model, "image"); ?>
                <?php $this->widget('CMultiFileUpload', array(
                        'name'      => 'image',
                        'accept'    => 'jpeg|jpg|gif|png', // useful for verifying files
                        'duplicate' => 'Duplicate file!', // useful, i think
                        'denied'    => 'Invalid file type',
                        // useful, i think
                    )); ?>
                <p class="help-block"><?php echo $error; ?></p>

                <?php if ($model->image):
                    $image = explode(',', $model->image);
                    foreach ($image as $k => $file):
                        ?>
                        <div class="clearfix"></div>
                        <div><?php echo $file; ?> &nbsp;
                            <a href="javascript:;" onclick="javascript:removeFile('<?php echo $file; ?>', <?php echo $k; ?>)" title="Remove" class="remove_<?php echo $k; ?>">Remove File</a></div>
                        <div class="clearfix"></div>
                    <?php
                    endforeach;
                endif;?>
            </div>
        </div>
    <?php } ?>

        <div class="form-actions no-margin-bottom">
            <?php echo CHtml::submitButton('Save', array('class' => 'btn btn-primary')); ?>
        </div>
</div>
<?php $this->endWidget(); ?>

<?php
if ($id) {
    $scriptDelete = '
        if("' . $model->image . '" != ""){
            function removeFile(fileName, pos) {
                var url = "' . Yii::app()->controller->createUrl('/admin/videoYoutube/deleteImage') . '";
                var imageID = "' . $model->meme_id . '";
                var colImage = "image";

                $.post(
                    url, { imageID: imageID, colImage: colImage, imageName: fileName, model: "Meme" },
                    function(data){
                        $("a.remove_"+pos).parent().remove();
                        parent.window.location = parent.window.location
                    }, "json"
                );

                return false;
            }
        }
        ';
    Yii::app()->clientScript->registerScript('scriptDelete', $scriptDelete, CClientScript::POS_END);
}